package com.lemon;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author xucong
 * @date 2021/4/8
 */
public class StudyApplication {



    public StudyApplication(){


    }

    public static void main(String[] args) {

        StudyApplication studyApplication = new StudyApplication();
        String path = studyApplication.getPath();

        String configData = "";

        String user_data = System.getenv("USER_DATA");
        if (StrUtil.isNotBlank(user_data)){
            configData = user_data;
        }else{
            File file = new File(path + "/cookie.conf");
            String config = FileUtil.readString(file, "utf-8");
            configData = config;
        }
        JSONArray userList = JSONUtil.parseArray(configData);
        for (int i = 0; i < userList.size(); i++) {
            JSONObject jsonObject = userList.getJSONObject(i);
            String name = jsonObject.getStr("name");
            String cookie = jsonObject.getStr("cookie");
            String edurefurl = jsonObject.getStr("edurefurl");

            StudentExcutor studentExcutor = new StudentExcutor(name,cookie,edurefurl);
            studentExcutor.start();
        }

    }


    public String getPath()
    {
        String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        if(System.getProperty("os.name").contains("dows"))
        {
            path = path.substring(1,path.length());
        }
        if(path.contains("jar"))
        {
            path = path.substring(0,path.lastIndexOf("."));
            return path.substring(0,path.lastIndexOf("/"));
        }
        return path.replace("target/classes/", "");
    }

}
